<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="制单日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD " value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="lsh" label="单号">
          <a-input v-model:value="formState.lsh" class="w-64" placeholder="单号" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2" type="primary" @click="onEditOpen"> 新增 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" column-code="000602" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === 0" color="blue">待确认</a-tag>
            <a-tag v-if="record.status === 1" color="green">已确认</a-tag>
            <a-tag v-if="record.status === 2" color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'lsh'">
            <PopoverTable :config="detailConfig" :params="{ lsh: record.lsh }" :width="1200">
              <template #bodyCell="{ column: popColumn, record: popRecord }">
                <template v-if="popColumn.dataIndex === 'ypmc'">
                  <MedicineInfo :value="{ mc: popRecord.ypmc, gg: popRecord.gg, cj: popRecord.cj }" />
                </template>
              </template>
              {{ record.lsh }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" :disabled="!!record.status" @click="onDetail(record)">明细</a-button>
              <a-button type="link" size="small" :disabled="!!record.status" @click="onEditOpen(record)">修改</a-button>
              <BaseButton confirm-text="是否确认?" :disabled="!!record.status" type="link" size="small" @click="onConfirm(record, 1)"> 确认 </BaseButton>
              <BaseButton confirm-text="是否作废?" :disabled="!!record.status" type="text" danger size="small" @click="onConfirm(record, 2)"> 作废 </BaseButton>
              <a-button type="link" size="small" @click="onPrint(record)"> 打印 </a-button>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <AddModal v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
// import useUserStore from '@/store/user'
import BaseButton from '@/components/BaseButton'
import AddModal from './AddModal'

const editOpen = ref(false)
const rowData = ref({})
const router = useRouter()
const formRef = ref(null)
// const { empId } = useUserStore()
const formState = ref({
  dateRange: [dayjs().subtract(1, 'month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/bloodm/BldStoreOutRecord/getBldStoreOutRecordList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1]
    }
  }
})

const detailConfig = ref({
  url: '/bloodm/BldStoreOutRecord/getBldStoreOutDetailList',
  columnCode: '000615',
  rowKey: 'lsh',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const onEditOpen = (record = {}) => {
  rowData.value = { ...record }
  editOpen.value = true
}

// 确认和作废
const onConfirm = async (record, status) => {
  const res = await post('/bloodm/BldStoreOutRecord/doUpdateBldStoreOutRecord', { lsh: record.lsh, status })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
// 明细
const onDetail = (record) => {
  router.push({
    path: '/blood-bank/bank-management/outbound-info',
    query: {
      lsh: record.lsh
    }
  })
}
// 打印
const onPrint = async (row) => {
  const { code, msg, data } = await post('/drugStorage/psiform/printWarehousingList', { rkdh: row.rkdh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
</script>
